/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package config;

import com.google.common.base.Function;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.reflect.FieldUtils;
import share.PGException;
import share.PGHelper;

/**
 *
 * @author KieuAnh
 */
public class CFTemp implements JSONable
{
    private static final Map<Class<?>, Function<Object, Object>> CONVERTERS = new HashMap();
    
    static
    {
        CONVERTERS.put(int.class, new Function<Object, Object>() {
            @Override
            public Object apply(Object f) {
                return PGHelper.toInteger(f);
            }
        });
        CONVERTERS.put(float.class, new Function<Object, Object>() {
            @Override
            public Object apply(Object f) {
                return PGHelper.toFloat(f);
            }
        });
        CONVERTERS.put(long.class, new Function<Object, Object>() {
            @Override
            public Object apply(Object f) {
                return PGHelper.toLong(f);
            }
        });
        CONVERTERS.put(String.class, new Function<Object, Object>() {
            @Override
            public Object apply(Object f) {
                return String.valueOf(f);
            }
        });
        CONVERTERS.put(String[].class, new Function<Object, Object>() {
            @Override
            public Object apply(Object f) {
                String fStr = String.valueOf(f);
                return fStr.split(";");
            }
        });
    }
    
    private CFTemp() {}
    static CFTemp parse(Map<String, Object> json)
    {
        CFTemp temp = new CFTemp();
        temp.deser(json);
        
        return temp;
    }
    
    public float DigestTimePerFish = 0;
    public float PenguinExpPerFish = 0;
    public int PenguinDefaultLevel = 0;
    public int BuyPenguinExp_Exp = 0;
    public int BuyPenguinExp_Cost = 0;
    public int BuyPenguinLevel_ExpPerCoin = 0;
    public int SessionTime = 0;
    public int NumberDailyQuest = 0;
    public int CotesEggs_Limit = 0;
    public int CotesEggs_Limit_Display = 0;
    public long MaxDogTime = 0;
    public long WakeDogFirstTime_Time = 0;
    public int TakeSnapshotFirstTimeInDay_Prize = 0;
    public int AutoSyncFriends_Day = 0;
    public long SyncFriendsCooldown = 0;
    public int AutoRemoveNPC_Level = 0;
    public int AutoRemoveNPC_Friends = 0;
    public int RandomizePrize_Expire = 0;
    public String Admin_UID = "";
    public String AdminTool_UID = "";
    public String SystemPasswordMD5 = "";
    public int AdultPenguin_Level = 0;
    public int MaxBuyRPByGoldPerDay = 0;
    public int MaxBuyRPByCoinPerDay = 0;
    public int NPCPoolSize = 0;
    public int MaxPenguin_Level = 0;
    public int CoinCardCoinPerDay = 0;
    public int CoinCardDuration = 0;
    public int CoinCardPrice = 0;
    public int MinigameTurnPerDay = 0;
    public int SaleOffKeyExpireInDay = 0;

    @Override
    public void deser(Map<String, Object> json) {
        for (Map.Entry<String, Object> tmpEntry : json.entrySet()) {
            String fieldName = tmpEntry.getKey();
            Object value = tmpEntry.getValue();
            
            Field field = FieldUtils.getField(getClass(), fieldName, true);
            if (field != null)
            {
                Class<?> fldClass = field.getType();
                if (CONVERTERS.containsKey(fldClass))
                {
                    try {
                        Function<Object, Object> converter = CONVERTERS.get(fldClass);
                        field.setAccessible(true);
                        field.set(this, converter.apply(value));
                    } catch (IllegalArgumentException ex) {
                        PGException.pgThrow(ex, "Invalid convert");
                    } catch (IllegalAccessException ex) {
                        PGException.pgThrow(ex, "Invalid convert");
                    }
                }
            }
        }
    }
}